$(document).ready(function () {
    $(".dp-article-box").fadeOut(0);
    $(".js-add-dp-box,.js-hf-article-pl").click(function () {
        $(".dp-article-box").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });
    blog_read_add();
    blog_stat();
    blog_hot();
    blog_comment();
});

/**
 * 博客阅读数量+1
 */
function blog_read_add() {
    var blogId = $("#blogId").val();
    $.ajax({
        url: '/blog/add/read/'+blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {

        },error:function (d) {
        }
    });
}

/**
 * 该博客统计数据
 */
function blog_stat() {
    var blogId = $("#blogId").val();
    $.ajax({
        url: '/blog/stat/' + blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code == 0) {
                var stat = d.stat;
                $("#readCount").text(stat.readCount);
                $("#collectCount").text(stat.collectCount);
                $("#praiseCount").text(stat.praiseCount);
                $("#praiseCountNum").text(stat.praiseCount);
                $("#collectCountNum").text(stat.collectCount);
            }
        },error:function (d) {
        }
    });
}

/**
 * 添加博客评论
 */
function addBlogComment(obj,blogId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;
            var content = $(obj).parent().children('textarea').val();
            if (!content) {
                layer.msg("评论内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/blog/add/comment/' + blogId,
                data: {"content": content},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code == 0) {
                        layer.msg("评论成功~",{icon:6});
                        blog_comment();
                    }else{
                        layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}

/**
 * 回复评论
 */
function replyBlogComment(obj,blogId,replyId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;

            var content = $(obj).parent().children('textarea').val();
            if(!content){
                layer.msg("回复内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/blog/add/comment/'+blogId,
                data:{"content":content,"replyId":replyId},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code==0) {
                        layer.msg("回复成功~",{icon:6});
                        blog_comment();
                    }else{
                        layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}

/**
 * 热门博客
 */
function blog_hot() {
    layui.use('laytpl', function(){
        var laytpl = layui.laytpl;
        var options = {
            dataType: "json",
            success: function (data) {
                if (data && data.code==0) {
                    var listData = {"list": data.list};
                    var getTpl = fiveBlogDataListTpl.innerHTML,
                        view = document.getElementById('fiveBlogListDataBody');

                    laytpl(getTpl).render(listData, function (html) {
                        view.innerHTML = html;
                    });
                }

            },
            error: function (e) {
            }
        };
        $("#fiveBlogHotForm").ajaxSubmit(options);
    })
}

/**
 * 该博客评论列表
 */
function blog_comment() {
    $("#commentsListBody").html("");
    layui.use('flow', function(){

        var flow = layui.flow;
        flow.load({
            elem: '#commentsListBody'
            ,done: function(page, next){
                var $ = layui.jquery;
                var lis = [];
                $.get('/blog/comments/'+$("#blogId").val(),{"number":page}, function(res){
                    if(res.page){
                        layui.each(res.page.records, function(index, item){
                            var replyId = item.replyId,reply_html='',user = res.usersMap[item.userId];

                            if(replyId){
                                var reply = res.usersMap[item.replyId];
                                reply_html ='<a href="/u/'+reply.username+'" target="_blank">@'+reply.username+'</a> ';;
                            }
                            var html= '<div class="pl-box-wrap  " >'+
                                '<div class="pl-box-top">'+
                                '<div class="dp-list-box">'+
                                '<div class="dl-user ">'+
                                '<ul>'+
                                '<li>'+
                                '<a href="/u/'+user.username+'" target="_blank">'+
                                '<img src="'+user.headimg+'">'+
                                '</a>'+
                                '</li>'+
                                '</ul>'+
                                '<div class="one-pl-content">'+
                                '<p class="content">'+
                                '<span class="name">'+user.username+'</span>'+
                                '<a href="/u/'+user.username+'" target="_blank"></a>&nbsp;&nbsp;'+
                                '<span class="author-content">'+reply_html+item.content+'</span>'+
                                '</p>'+
                                '</div>'+
                                '</div>'+
                                '</div>'+
                                '</div>'+

                                '<div class="pl-box-btm">'+
                                '<div class="article-type pull-right">'+
                                '<div class="icon-like-prompt">'+
                                '<i class="icon icon-like active"></i><span class="c1">+1</span>'+
                                '</div>'+
                                '<div class="icon-no-like-prompt">'+
                                '<i class="icon icon-no-like active"></i><span class="c1">+1</span>'+
                                '</div>'+
                                '<ul>'+
                                '<li class="js-icon-like" data-type="like">' +
                                '<i class="icon icon-like "></i><span class="like">2</span></li>'+
                                '<li class="js-no-icon-like" data-type="no-like">' +
                                '<i class="icon icon-no-like "></i><span class="like">1</span></li>'+
                                '</ul>'+
                                '</div>'+
                                '<a onclick="showPTextArea(this)"><div class="btn-dp transition js-add-dp-box"><i class="icon icon-dp"></i>回复</div></a>'+
                                '<div class="pl-form-box dp-article-box">'+
                                '<textarea class="form-control" placeholder="请文明回复~"></textarea>'+
                                '<button class="btn btn-article js-article-dp" onclick="replyBlogComment(this,'+item.tweetId+',\''+item.userId+'\')">回复</button>'+
                                '</div>'+
                                '</div>'+
                                '</div>';

                            lis.push(html);
                        });
                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }
        });
    });
}

/**
 * 给博客点赞或取消点赞
 * @param blogId
 */
function blog_praise(blogId) {
    if(!logined()){
        showLogin();
        return;
    }
    if($(".js-like-article-zan").hasClass("praise-box-active")){
        blog_praise_del(blogId)
    }else{
        blog_praise_add(blogId);
    }
}


/**
 * 给该博客点赞
 */
function blog_praise_add(blogId) {
    $.ajax({
        url: '/blog/add/praise/'+blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                $(".js-like-article-zan").addClass("praise-box-active");
                blog_stat();
            }
        },error:function (d) {
            $(".js-like-article-zan").removeClass("praise-box-active");
        }
    });
}

/**
 * 取消点赞
 * @param blogId
 */
function blog_praise_del(blogId) {
    $.ajax({
        url: '/blog/cut/praise/'+blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                $(".js-like-article-zan").removeClass("praise-box-active");
                blog_stat();
            }
        },error:function (d) {
            $(".js-like-article-zan").removeClass("praise-box-active");
        }
    });
}

/**
 * 收藏或取消收藏博客
 * @param blogId
 */
function blog_collect(blogId) {
    if(!logined()){
        showLogin();
        return;
    }
    if($(".js-like-article-collect").hasClass("collect-box-active")){
        blog_collect_del(blogId);

    }else{
        blog_collect_add(blogId);
    }
}

/**
 * 收藏博客
 */
function blog_collect_add(blogId) {
    $.ajax({
        url: '/blog/add/collect/'+blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                $(".js-like-article-collect").addClass("collect-box-active");
                blog_stat();
            }
        },error:function (d) {
            $(".js-like-article-collect").removeClass("collect-box-active");
        }
    });
}

/**
 * 取消收藏博客
 * @param blogId
 */
function blog_collect_del(blogId) {
    $.ajax({
        url: '/blog/cut/collect/'+blogId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                $(".js-like-article-collect").removeClass("collect-box-active");
                blog_stat();
            }
        },error:function (d) {
            $(".js-like-article-collect").removeClass("collect-box-active");
        }
    });
}

function showPTextArea(obj) {
    if($(obj).next('div').css("display")=='none'){
        $(obj).next().css({"display":"block"});

    }else{
        $(obj).next().css({"display":"none"});
    }

}